package com.api.shopapi.mapper;

import com.api.shopapi.entity.ArticleClass;
import com.api.shopapi.entity.dto.ArticleClassDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ArticleClassMapper {

    @Insert("insert into article_class (ArticleClassCode,ParentArticleClassCode,Name,Icon,Sort,Status,GovernmentCode) values " +
            "(#{articleClass.ArticleClassCode},#{articleClass.ParentArticleClassCode},#{articleClass.Name},#{articleClass.Icon}," +
            "#{articleClass.Sort},#{articleClass.Status},#{articleClass.GovernmentCode})")
    int insert(@Param("articleClass") ArticleClass articleClass);


    @Update("update article_class set ParentArticleClassCode=#{articleClass.ParentArticleClassCode},Name=#{articleClass.Name}," +
            "Icon=#{articleClass.Icon},Sort=#{articleClass.Sort},Status=#{articleClass.Status} " +
            "where ArticleClassCode=#{articleClass.ArticleClassCode}")
    int update(@Param("articleClass") ArticleClass articleClass);

    @Select("select * from article_class where ArticleClassCode=#{articleClassCode} limit 1")
    ArticleClass getByArticleClassCode(@Param("articleClassCode") String articleClassCode);

    @Select("select * from article_class where ParentArticleClassCode=#{parentArticleClassCode} and status =1 order by Sort desc")
    List<ArticleClass> getListByParentArticleClassCode(@Param("parentArticleClassCode") String parentArticleClassCode);


    //查询父级Code下所有子集（包含状态被禁用的）
    @Select("select * from article_class where ParentArticleClassCode=#{parentArticleClassCode} order by Sort desc")
    List<ArticleClass> getAllListByParentArticleClassCode(@Param("parentArticleClassCode") String parentArticleClassCode);

    //查看政府下所有状态正常的一级分类
    @Select("select * from article_class where ParentArticleClassCode= '00000000-0000-0000-0000-000000000000' " +
            "and GovernmentCode=#{governmentCode} " +
            "and status = 1 order by Sort desc")
    List<ArticleClassDTO> getFirstClassList(@Param("governmentCode") String governmentCode);


    @Select("<script>" +
            "select * from article_class where GovernmentCode = #{governmentCode} " +
            "and ParentArticleClassCode = '00000000-0000-0000-0000-000000000000' " +
            "<if test ='name !=null and name!=\"\" '>" +
            "and Name like concat('%',#{name},'%') " +
            "</if>" +
            "order by Sort desc " +
            "</script>")
    List<ArticleClassDTO> getPage(@Param("name") String name, @Param("governmentCode") String governmentCode);


}
